package com.youlu.campus.admin.auth.shiro.permission;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.util.AntPathMatcher;
import org.apache.shiro.util.PatternMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * Created by zhuhuaiqi on 2018/03/13.
 *
 * @author zhuhuaiqi
 * @since 2018/03/13
 */
public class UriPermission implements Permission, Serializable {

    protected static final Logger logger = LoggerFactory.getLogger(UriPermission.class);

    private static final PatternMatcher URI_PATH_MATCHER = new AntPathMatcher();

    private String uriPattern;

    public UriPermission(String uriPattern) {
        this.uriPattern = uriPattern;
    }

    @Override
    public boolean implies(Permission p) {
        if (!(p instanceof UriPermission)) return false;
        UriPermission uriPermission = (UriPermission) p;
        boolean match = URI_PATH_MATCHER.matches(uriPattern, uriPermission.getUri());
        //logger.info("UriPermission implies uri:{}, up.getUrl:{}, match:{}", uriPattern, uriPermission.getUri(), match);
        return match;
    }

    public String getUri() {
        return uriPattern;
    }

    public void setUri(String uri) {
        this.uriPattern = uri;
    }
}
